package com.example.use;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;

import java.util.ArrayList;

public class RecActivity extends AppCompatActivity implements View.OnClickListener {
    ContentResolver resolver;
    Cursor cursor;
    CursorAdapter adapter = null;
    Uri uri;
    Button b1, b2, b3;
    //在此我们可以针对数据改变后做一些操作，比方说Adapter.notifyDataSetChanged()等，根据业务需求来定主要是通知应用数据发生改变
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            cursor = resolver.query(uri, null, null, null, null);

            adapter = null;
            adapter.changeCursor(cursor);     //设置要通知的组件
        }

        ;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rec);
        uri = Uri.parse("content://com.example.studentProvide");
        resolver = getContentResolver();
        if (resolver == null || uri == null) {

        }
        resolver.registerContentObserver(uri, true, new MyObserver(handler));
    }


    public void quer() {
        resolver.query(uri, new String[]{"id=1236"}, null, null);
       // resolver.query(uri,"id=?",new String[]{"232"},null);
        // resolver.query(uri,"id=?",new String[]{"2323"},null,null)


    }

    public void inser() {

        ContentValues s = new ContentValues();
        s.put("name", "刘宇");
        s.put("num", "293293");
        s.put("age", 20);
        resolver.insert(uri, s);

    }

    public void delete() {


        resolver.delete(uri, "id=?", new String[]{"23456"});

    }

    private void initData() {
        b1 = findViewById(R.id.t1);
        b2 = findViewById(R.id.t2);
        b3 = findViewById(R.id.t3);
        /*注册内容观察者
        //resolver.registerContentObserver(STUDENT_ALL_URI,true,new PersonOberserver(handler));

        adapter=new MyAdapter(MainActivity.this,cursor);
        lvShowInfo.setAdapter(adapter);*/
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //初始化
            case R.id.t1:
                addstu();
                break;
                //查询
            case R.id.t2:
                quer();
                break;
                //删除一条记录
            case   R.id.t3:
               delete();
                break;
        }
    }

    private void addstu()
    {

        ArrayList<Student> students = new ArrayList<Student>();
        Student student1 = new Student("刘宇","123",20);
        Student student2 = new Student("柳岩","124",28);
        Student student3 = new Student("杨幂","125",26);
        Student student4 = new Student("张馨予","125",28);

        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);
         //使用foreacht添加数据
        for (Student Student : students)
        {
            ContentValues values = new ContentValues();
            values.put("name", Student.getName());
            values.put("num",Student.getNum());
            values.put("age", Student.getAge());

            resolver.insert(uri, values);
        }

    }

}